package utility;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The FileManager class provides a convenient way
 * to access and process files.
 * Makes assumptions that everything works as expected,
 * so it's not very safe in a real application.
 * 
 * @author Kevin Dolan
 */
public class FileManager {
	
	/**
	 * Read a file and return its contents as a list of lines
	 * @param file the file to read
	 * @return	   a list where each element is a line of the file
	 */
	public static List<String> readLines(File file) {
		List<String> result = new ArrayList<String>();
		try { 
			BufferedReader in = new BufferedReader(new FileReader(file)); 
			String str; 
			while ((str = in.readLine()) != null) { 
				result.add(str);
			} in.close(); 
		} catch (IOException e) { } 
		return result;
	}
	
	/**
	 * Write plain text to a file
	 * @param text the text to write
	 * @param file the file to write to
	 */
	public static void writeTextToFile(String text, File file) {
		try {
	        BufferedWriter out = new BufferedWriter(new FileWriter(file));
	        out.write(text);
	        out.close();
	    } catch (IOException e) {
	    	e.printStackTrace();
	    }
	}
	
}
